from django.db import models


class Terminal(models.Model):
    """设备
    车牌颜色
    未上牌时，取值为0
    id          color
    --          ------
    1           蓝
    2           黄
    3           黑
    4           白
    9           其他

    车辆标识
    车牌颜色为0时，表示车辆VIN；
    否则，表示公安交通管理部门颁发的机动车号牌
    """

    StatusChoice = (
        ('INIT', '未注册'),
        ('REGISTED', '已注册'),
    )
    DeviceTypeChoice = (
        ('YB', '自主终端'),
        ('808', '标准808设备'),
        ('KY', '开易设备')
    )
    LicenseColorChoice = (
        (1, '蓝'),
        (2, '黄'),
        (3, '黑'),
        (4, '白'),
        (9, '其他'),
    )
    # 808 终端注册
    province = models.CharField(max_length=10, blank=True, null=True, verbose_name='省域ID')
    city = models.CharField(max_length=10, blank=True, null=True, verbose_name='市县域ID')
    manufactor = models.CharField(max_length=5, blank=True, null=True, verbose_name='制造商ID')
    model = models.CharField(max_length=20, blank=True, null=True, verbose_name='终端型号')
    uid = models.CharField(max_length=30, blank=True, null=True, verbose_name='终端ID')
    # 字段请查找相关车辆
    # license_color = models.SmallIntegerField(choices=LicenseColorChoice, default=LicenseColorChoice[1][0], verbose_name='车牌颜色')
    # license_number = models.CharField(max_length=20, blank=True, null=True, verbose_name='车辆标识')
    # 808 终端属性应答
    category = models.CharField(max_length=20, blank=True, null=True, verbose_name='终端类型')
    imei = models.CharField(max_length=17, blank=True, null=True, verbose_name='终端IMEI号15-17位')
    iccid = models.CharField(max_length=20, blank=True, null=True, verbose_name='SIM卡ICCID')
    hardware_version = models.CharField(max_length=30, blank=True, null=True, verbose_name='硬件版本')
    firmware_version = models.CharField(max_length=50, blank=True, null=True, verbose_name='固件版本')
    communication = models.CharField(max_length=20, blank=True, null=True)
    status = models.IntegerField(blank=True, null=True, verbose_name='终端注册应答结果')
    auth_code = models.CharField(max_length=20, blank=True, null=True, verbose_name='鉴权码')
    params_gnss = models.CharField(max_length=20, blank=True, null=True, verbose_name='GNSS模块属性')
    params_comm = models.CharField(max_length=20, blank=True, null=True, verbose_name='通信模块属性')
    # 自定义字段
    channel_count = models.SmallIntegerField(default=1, verbose_name='通道数量')
    device_type = models.CharField(max_length=10, choices=DeviceTypeChoice, default=DeviceTypeChoice[0][0], verbose_name='设备类型')
    custom_status = models.CharField(max_length=10, choices=StatusChoice, default=StatusChoice[0][0], verbose_name='状态')
    created_time = models.DateTimeField(auto_now_add=True, editable=False)
    modified_on = models.DateTimeField(auto_now=True)

    @property
    def gnss_list(self):
        return self.params_gnss.split('-')

    @property
    def comm_list(self):
        return self.params_comm.split('-')

    @property
    def category_list(self):
        return self.category.split('-')

    def __str__(self):
        return self.imei

    class Meta:
        db_table = 'terminal'


class TerminalParams(models.Model):
    """设备参数
    """

    DataTypeChoice = (
        (0, '字符串'),
        (1, '单字节'),
        (2, '双字节'),
        (4, '四字节'),
        (8, '八字节'),
    )

    terminal = models.ForeignKey(Terminal, on_delete=models.CASCADE, db_index=True, verbose_name='企业')
    uid = models.IntegerField(default=0, verbose_name='参数ID，0x0000-0xFFFF，参考JT808文档18页')
    name = models.CharField(max_length=50, verbose_name='参数名称-相当于注释')
    data = models.CharField(max_length=50, verbose_name='参数数据，类型请查看`data_type`字段')
    data_type = models.SmallIntegerField(choices=DataTypeChoice, default=DataTypeChoice[0][0], verbose_name='`value`字段中的数据类型')
    created_time = models.DateTimeField(auto_now_add=True, editable=False)
    modified_on = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = 'terminal_params'


class TerminalControl(models.Model):
    terminal = models.ForeignKey(Terminal, on_delete=models.CASCADE, db_index=True)
    params = models.CharField(max_length=50)

    class Meta:
        db_table = 'terminal_control'
